﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PingLogger;
using System.Net;
using System.Diagnostics;

/*
     This file is part of Plogger.

    Plogger is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Plogger is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Plogger.  If not, see <http://www.gnu.org/licenses/>.
  
 */

namespace Plogger
{
    public partial class PloggerForm : Form
    {
        private int _handleNum = 0;

        public PloggerForm()
        {
            InitializeComponent();
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            int NoP = 10000;
            int MoH = 10000;
            string address = txtBxAddress.Text;
            this._handleNum++;
            Pinger p = null;
            Tracerter t = null;
            FileWriter fw = new FileWriter();

            if (!Validator.ReadInt(txtBxNoP.Text, out NoP) && NoP > 0) 
            {
                MessageBox.Show("Max number of hops must be an integer larger than 0.", "Error");
                new CancelEventArgs(true);
            }

            if (chkBxTracert.CheckState == CheckState.Checked)
            {
                if (!Validator.ReadInt(txtBxMoH.Text, out MoH))
                {
                    MessageBox.Show("Number of pings must be an integer", "Error");
                }
                try
                {
                    t = new Tracerter(address, MoH);
                    t.StartTracert();
                }

                catch
                {
                    Tracerter tr = new Tracerter(address, MoH);
                    t = tr;
                    tr = null;
                    GC.Collect();
                    t.StartTracert();
                }
            }

            p = new Pinger(new IntPtr(this._handleNum), address, NoP);

            p.StartPing();

            if (t != null)
            {
                fw.TracertResult = t.Result;
            }

            fw.PingResult = p.PingResult;

            fw.WriteToFile();
            Process.Start(fw.FileName);

            p = null;
            t = null;
            fw = null;
            GC.Collect();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtBxAddress.Text = "ping.sunet.se";
            txtBxMoH.Text = "30";
            txtBxNoP.Text = "1000";
            chkBxTracert.Checked = true;
        }

        private void lnkWebPage_Click(object sender, EventArgs e)
        {
            Process.Start(lnkWebPage.Text);
        }
    }
}
